package com.my.service.impl;


import com.my.service.ShopCartService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Service
public class ShopCartServiceImpl implements ShopCartService {

    @Resource
    private LoadBalancerClient loadBalancerClient;//ribbon负载均衡器

    private String getUrl(String str) {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("shopcart-provider");
        return "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/shopCart/" + str;
    }

    @Override
    public String showGoods(Integer currentPage, Integer pageSize, String sort, Boolean asc, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("currentPage", currentPage);
        map.add("pageSize", pageSize);
        map.add("sort", sort);
        map.add("asc", asc);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("show"), request, String.class);
        return response.getBody();
    }

    @Override
    public String addToShopCart(Integer goodsId, Integer num, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("goodsId", goodsId);
        map.add("num", num);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("add"), request, String.class);
        return response.getBody();
    }

    @Override
    public String updateShopCart(Integer cartId, Integer num, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("cartId", cartId);
        map.add("num", num);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("update"), request, String.class);
        return response.getBody();
    }

    @Override
    public String deleteFromShopCart(Integer cartId, HttpServletRequest req) {
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("token", req.getHeader("token"));
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("cartId", cartId);
        HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(map, headers);
        ResponseEntity<String> response = restTemplate.postForEntity(getUrl("delete"), request, String.class);
        return response.getBody();
    }
}
